package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.portal.repository.OuterTeacherRepository;
import com.yuyou.fn.portal.vo.OuterTeacher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@EventHandler(tables = "yyfn.teacher")
@Component
public class OuterTeacherUpdater implements IEventHandler{

    private final static Logger log = LoggerFactory.getLogger(OuterTeacherUpdater.class);

    @Resource
    private OuterTeacherRepository outerTeacherRepository;

    @Resource
    private IAreaTeamService areaTeamService;


    @Override
    public void insert(String tableName, JSONArray rowList) {
        List<OuterTeacher> outerTeacherList = toTeacher(rowList, false );
        outerTeacherRepository.save(outerTeacherList);

    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {

        List<OuterTeacher> outerTeacherList = toTeacher(rowList, false );
        outerTeacherRepository.save(outerTeacherList);

    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

        List<OuterTeacher> outerTeacherList = toTeacher(rowList, true );
        outerTeacherRepository.delete(outerTeacherList);

    }

    private List<OuterTeacher> toTeacher(JSONArray rowList , boolean isDelete) {
        List<OuterTeacher> outerTeacherList = new ArrayList<>(rowList.size());
        for (int i = 0; i < rowList.size(); i++) {
            JSONObject row = processFiled(rowList.getJSONObject(i) );
            OuterTeacher outerTeacher = row.toJavaObject(OuterTeacher.class);
            if(!isDelete){
                fillExtra(outerTeacher);
            }
            outerTeacherList.add(outerTeacher);
        }
        return outerTeacherList;
    }
    private JSONObject processFiled(JSONObject row) {
        JSONObject nRow = new JSONObject();
        for(String key : row.keySet()){
            nRow.put(Utils.camelCase(key),row.get(key));
        }
        return nRow ;
    }

    private void fillExtra(OuterTeacher outerTeacher) {
        //填充城市
        if (outerTeacher.getAreaTeamId()!=null){
            AreaTeam areaTeam=areaTeamService.findById(outerTeacher.getAreaTeamId());
            outerTeacher.setCity(areaTeam.getCity());
        }
        if (outerTeacher.getJobNature()!=null){
            if (outerTeacher.getJobNature().equals("1")){
                outerTeacher.setTeacherName(outerTeacher.getTeacherName()==null?"":outerTeacher.getTeacherName().charAt(0)+"老师");
            }

        }

    }

}
